
//
//  RootViewController.m
//	Abstract: the main window controller.
//  IWeighMyself
//
//  Created by Francisco Gindre on 7/28/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "RootViewController.h"
#import "LogNewWeightViewController.h"
#import "IWeighMyselfAppDelegate.h"
#import "WeightHistoryViewController.h"
#import "ShareWeightModeViewController.h"

@implementation RootViewController

@synthesize settingsButton;
@synthesize menuList,sectionList;
@synthesize settingsView;
@synthesize appDelegate;

#pragma mark -
#pragma mark View constants

// defining constants that format tables and sections
// define default number of sections on the table view
#define DEFAULT_NUMBER_OF_SECTIONS 3
//sections per table
#define WEIGHT_LOG_SECTION 0
// doesn't have multiple cells
#define HISTORY_SECTION 1
// doesn't have multiple cells
#
#define SHARING_SECTION 2
	//define index for rows in sharing section
#define SHARE_WITH_MYSELF_INDEX 0
#define SHARE_WITH_DEFAULT_CONTACT_INDEX 1
#define SHARE_WITH_NEW_CONTACT_INDEX 2


// number of rows per section
#define ROWS_IN_WEIGHT_LOG_SECTION 1
#define ROWS_IN_HISTORY_SECTION 1
#define ROWS_IN_SHARING_SECTION 3

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.appDelegate= [[UIApplication sharedApplication]delegate];
	if ([appDelegate isFirstTimeSetup]) {
		
		
		[self openSettingsView:self];
	}
	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
   //  
	
	//Create the "Settings" button with a plain style.
	
	self.settingsButton = [[UIBarButtonItem alloc]initWithTitle:NSLocalizedString(@"SettingsBarButtonItem",@"") 
														  style:UIBarButtonItemStylePlain target:self action:@selector(openSettingsView:)];
	
	//Set the settingsButton to the rightBarButtonItem to make it show on the right part of the navigation bar
	self.navigationItem.rightBarButtonItem = self.settingsButton;
	
	//set the application title
	self.navigationItem.title = @"iWeighMyself";
	
	// initialize the menu list
	self.menuList = [NSMutableArray array];
	
	// init the section list
	self.sectionList = [NSMutableArray array];
	
	//set the strings needed for this view
	
	// set the new weight section
	[self.sectionList addObject:NSLocalizedString(@"NewWeightSection",@"")];
	
	//set the new weight section buttons
	[self.menuList addObject:[NSMutableArray arrayWithObject:NSLocalizedString(@"LogNewWeightButton",@"")]];
	 
	//set the history section
	[self.sectionList addObject:NSLocalizedString(@"ShareWeightSection",@"")];
	
	//set the history section button strings
	[self.menuList addObject:[NSMutableArray arrayWithObject:NSLocalizedString(@"ViewEditHistoryButton",@"")]];
	
	//set the sharing section string 
	
	[self.sectionList addObject:NSLocalizedString(@"ShareWeightSection",@"")];
	
	//set the sharing section button string
	[self.menuList addObject:[NSMutableArray arrayWithObjects: NSLocalizedString(@"ShareWithMyselfButton",@""),
							  NSLocalizedString(@"ShareWithDefaultContactButton",@""),
							  NSLocalizedString(@"ShareWithNewContactButton",@""),nil]];
	
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //return the number of items in the section array
	return sectionList.count;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	
	return [(NSMutableArray*)[menuList objectAtIndex:section] count];
}


// set section text 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	return [self.sectionList objectAtIndex:section];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"AnIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	
	// configure the cell with the contents that it should have
	[cell.textLabel setText:[(NSMutableArray*)[menuList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
	
	
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

/*
 Subsequent views load lazily to reduce memory usage. 
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
	
	
	// go to the corresponding section pushing the correct view controller.
	
	switch (indexPath.section) {
	
		case WEIGHT_LOG_SECTION:
			[self openLogNewWeightView];
			
						break;
		case HISTORY_SECTION:
		//text label for history section
			[self openWeightHistoryView];
		
			break;
		case SHARING_SECTION:
		// show cells inside sharing section of table view
		switch (indexPath.row) {
			
			case SHARE_WITH_MYSELF_INDEX:
				
				break;
			case SHARE_WITH_DEFAULT_CONTACT_INDEX:
				
				break;
			case SHARE_WITH_NEW_CONTACT_INDEX:
				
				break;
				
			default:
				break;
		}
		
		
		break;
		
	default:
		break;
	}
	
}

#pragma mark -
#pragma mark Methods that open new views
/*
 this method creates up the settings view controllers and pushes it to the navigation controller. It is called whenever
 the settings view needs to be displayed. This method also makes sure the view isn't created more than once.
 */

- (void)openSettingsView:(id)sender {
	
	//Navigation Logic - creates and push a new settings view controller
	
	//if the view wasn't created before, create it
	if (self.settingsView==nil) {
		
		// Get the viewController needed from the bundle
		NewUserViewController *settingsViewController = [[NewUserViewController alloc] initWithNibName:@"NewUserViewController" bundle:nil];
		
		
		
		
		// assign the settingsViewController instance to the settingsView property
		self.settingsView = settingsViewController;
		
		[self.settingsView retain];
		
		//set the title of the settings view
		self.settingsView.title = NSLocalizedString(@"SettingsBarButtonItem",@"");
		
		[settingsViewController release];
		
	}
	
	//Now set up an animation
	[self.navigationController pushViewController:self.settingsView animated:YES];
	
	[self.settingsView release];
	self.settingsView= nil;
	
	
}
/*
 Creates and pushes the LogNewWeightViewController to the navigationController
 */

- (void)openLogNewWeightView {
	LogNewWeightViewController *logNewWeightViewController = [[LogNewWeightViewController alloc] 
															  initWithNibName:@"LogNewWeightViewController" bundle:nil];
	// ...
	// Pass the selected object to the new view controller.
	[self.navigationController pushViewController:logNewWeightViewController animated:YES];
	[logNewWeightViewController release];
	
}


/*
 creates a WeightHistoryViewController lazily and pushes int to the navigation controller. 
 */

-(void) openWeightHistoryView {
	
	WeightHistoryViewController *weightHistoryViewController = [[WeightHistoryViewController alloc] 
																initWithNibName:@"WeightHistoryViewController" bundle:nil];
	// ...
	// Pass the selected object to the new view controller.
	[self.navigationController pushViewController:weightHistoryViewController animated:YES];
	
	//release to reduce memory footprint
	[weightHistoryViewController release];
}

/* 
 Displays a MFMailComposeViewViewController configured to the user's interface
 */
-(void)openShareWithMyselfView {
	//MFMailComposeViewController *mailViewController  = [[MFMailComposeViewController alloc]initWithRootViewController:self ];
	//[mailViewController setSubject:
}

#pragma mark -
#pragma mark handle messaging
//-(void)openMessageUIWithContact:(
#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}


// So release any properties that are loaded in viewDidLoad or can be recreated lazily.
//
- (void)viewDidUnload
{
	[super viewDidUnload];
	
	self.menuList = nil;
}


- (void)dealloc {
    [super dealloc];
}




@end

